package com.asiainfo.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.logging.Logger;
/*
 * 
 * 数据库连接工具类
 * 
 */
public class DB_Utils {
	public static String DRIVER;
	public static String URL;
	public static String USERNAME;
	public static String PASSWORD;
	//加载配置文件
	private static ResourceBundle rb = ResourceBundle.getBundle("com.asiainfo.database.db-config");
	//初始化参数，从配置文件读取
	static {
		URL = rb.getString("jdbc.url");
		DRIVER = rb.getString("jdbc.driver");
		USERNAME = rb.getString("jdbc.username");
		PASSWORD = rb.getString("jdbc.password");
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			Logger logger = Logger.getLogger(""); 
			logger.warning("驱动类没有找到");
			e.printStackTrace();
		}
	}
	//无参构造函数
	private DB_Utils() {
	}
	//获取数据库连接
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (SQLException e) {
			Logger logger = Logger.getLogger(""); 
			logger.warning("SQL异常");
			e.printStackTrace();
		}
		return conn;
	}
	//关闭数据库连接
	public static void close(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			Logger logger = Logger.getLogger(""); 
			logger.warning("SQL异常");
			e.printStackTrace();
		}
	}
}
